package com.gengzp.mq.client.runner;

import com.gengzp.mq.client.core.MqGlobalVariableManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName MqCommandLineRunner
 * @Description mq客户端启动时执行方法
 * @Author gengzp
 * @Date 2025/9/4 20:25
 */
@Component
public class MqCommandLineRunner implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(MqCommandLineRunner.class);

    @Autowired
    private MqGlobalVariableManager mqGlobalVariableManager;

    @Override
    public void run(String... args) throws Exception {
        // 取出所有的消息监听线程
        Map<String, Thread> mqListenerThreadMap = mqGlobalVariableManager.getMqListenerThreadMap();
        if (mqListenerThreadMap == null || mqListenerThreadMap.isEmpty()) {
            return;
        }

        // 启动所有监听线程
        for (Map.Entry<String, Thread> entry : mqListenerThreadMap.entrySet()) {
            entry.getValue().start();
            logger.info("检测到订阅 gengzp-mq 主题: {} 的消息监听器: {}, 消息监听线程启动成功",
                    entry.getKey(), mqGlobalVariableManager.getMqListenerBeanNameByTopic(entry.getKey()));
        }

    }

}
